# -*- coding: utf-8 -*-

import json
from django.http import HttpResponse
from django.shortcuts import render
from testdemo.DataBean import People
from testdemo.com.dao.UserDao import UserDao
from django.core import serializers
from TestModel.models import *

def data(request):
    context = {};
    list = [];
    for i in range(0,3):
       dict = {'name': '北京','city':'北京市','poster':'1000'};
       list.append(dict)
    d1 = People("北京","北京市","1000");
    d2 = People("南京", "南京市", "1001");
    d3 = People("上海", "上海市", "1002");
    d4 = People("菏泽", "菏泽市", "1003");
    list.append(d1)
    list.append(d2)
    list.append(d3)
    list.append(d4)
    context['list'] = list;
    return render(request, 'data.html', context)
    #http://peak.telecommunity.com/dist/ez_setup.py 使用这个安装（64位系统必须用这个）
def query(request):
    context = {};
    list = [];
    uDao = UserDao("select * from users");
    list = uDao.query();
    context['list'] = list;
    return render(request, 'query.html', context)
def querySearch(request):
    request.encoding = 'utf-8'
    context = {};
    list = [];

    context['list'] = list;
    sql = "select * from users where 1=1";
    if 'uname' in request.GET:
        uname = request.GET['uname'].encode('utf-8');
        if not uname.strip():
            print 'not null=='+uname+'---';
        else:
            sql = sql + " and name=" + uname;
            print 'not';
    if 'age' in request.GET:
        age = request.GET['age'];
        if (not age is None and age.strip()):
            print 'age='+age+'****'
            sql = sql + " and age=" + age;
        else:
            print 'age=' + age + '****----'
            sql = sql + " and age=" + age;

    print 'sql='+sql;
    try:
        uDao = UserDao(sql);
        list = uDao.query();
    except:
        print 'error!'
    context['list'] = list;
    return render(request, 'query.html', context)

def jsonn(request):
    list = ["name","id","age"]
    response_data = {}
    response_data['result'] = 'failed'
    response_data['message'] = 'You messed up'
    response_data['list'] = list
    return HttpResponse(json.dumps(response_data), content_type="application/json")